// pages/SubmitOrder/submitOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialogFlag: false, //微信手续费弹框
    payFlag: false,
    flag: false, //默认公积金关闭
    pictureUrl: '/image/'
  },
  refreshPay() {
    this.payBtn()
  },
  // 取消支付
  closePay() {
    wx.redirectTo({
      url: `../orderMsg/orderMsg?order_id=${this.data.orderid}&fromTo=true`
    })
  },
  // 点击消息弹框
  explanation() {
    this.setData({
      dialogFlag: true
    })
  },
  //点击支付（二维码）
  moreArticles(e) {
    let src = Number(e.currentTarget.dataset.id)
    
    wx.navigateTo({
      url: `../officialAccount/officialAccount?src=${src}`,
    })

  },

  // 点击支付
  payBtn() {
    wx.showLoading({
      title: '请求中',
      mask: true
    })
    app.aldstat.sendEvent('订单支付-支付按钮用户数', '订单支付-支付按钮用户数');

    let that = this
    app.request('daijiao/order/pay', {
      order_id: this.data.orderid
    }, true).then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        const obj = {
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
        };

        wx.requestPayment({
          ...obj,
          success(res) {
            app.aldstat.sendEvent('支付成功用户数', '支付成功用户数');
            wx.redirectTo({
              // url: `../orderMsg/orderMsg?order_id=${that.data.orderid}&fromTo=true`
              url: `../payment/payment?order_id=${that.data.orderid}&fromTo=true`
            })
          },
          fail(res) {
            that.setData({
              payFlag: true
            })
          }
        })
      }else{
        wx.showModal({
          content: res.error_message,
          showCancel:false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    app.request('daijiao/order/show_new', {
      order_id: options.orderid
    }, true).then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        res.data.shebao.city = res.data.city
        res.data.shebao.detail = res.data.detail
        this.setData({
          list: res.data,
          flag: res.data.gongjijin instanceof Array,
          orderid: options.orderid,
          userInfo: app.globalData.userInfo
          // orderNumer: res.data.order_id,//订单号
          // city: res.data.city,//缴纳城市
          // dates:res.data.dates,//缴纳详情
          // detail: res.data.detail,//费用明细
          // except: res.data.except_dates,//无法缴纳的月份
          // gongjijin_amount: res.data.gongjijin_amount,//公积金费用
          // gongjijin_count: res.data.gongjijin_count,//公积金缴纳时长
          // name: res.data.name,//姓名
          // shebao_amount: res.data.shebao_amount,//社保费用
          // shebao_count: res.data.shebao_count,//社保缴纳时长
          // service: res.data.service,//服务费
          // channel: res.data.channel,//通道费
          // count: res.data.count,//购买时长
          // total: res.data.total,//共计
        })
      }
    })
  },
})